package com.beilang.hotline.module.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.module.model.ModuleModel;
import com.beilang.hotline.module.to.SearchTO;


@SuppressWarnings("unchecked")
public class ModuleDAO extends PaginationBaseDAO {
	public ModuleModel createModule(ModuleModel moduleModel) {
        moduleModel.setDeleteInd("N");
        return (ModuleModel)this.save(moduleModel);
	}
	
	public ModuleModel saveModule(ModuleModel moduleModel) {
		return (ModuleModel)this.save(moduleModel);
	}
	
	public void deleteModule(Long moduleId){
		ModuleModel module= this.getModule(moduleId);
        module.setDeleteInd("Y");
        this.save(module);
	}
	
	public ModuleModel getModule(Long moduleId){
		return (ModuleModel)this.get(ModuleModel.class, moduleId);
	}
	
	public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap(); 
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String moduleName=StringUtil.convertEmpty2Null(searchTO.getModuleName());
        if(moduleName!=null)moduleName="%"+moduleName+"%";
        map.put("moduleName", moduleName);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchModuleCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String moduleName=StringUtil.convertEmpty2Null(searchTO.getModuleName());
        if(moduleName!=null)moduleName="%"+moduleName+"%";      
        map.put("moduleName", moduleName);
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchModuleList", map);
    }
    
    protected  String mapSortField(String dataField){
    	if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("sysName")) return "system_name";
    	if(dataField.equalsIgnoreCase("moduleName")) return "module_name";
    	return dataField;
    }

    public void deleteAllSysModules(Long sysId){
        Map map = new HashMap();
        map.put("sysId", sysId);
        getSqlMapClientTemplate().update("deleteAllSysModules",map);
    }

}
